import { assetManager, Prefab, instantiate, find, log, resources, director } from 'cc';
import losKuangDian from 'db://assets/kuangDian/losKuangDian';
import { losChannel } from '../SDK/losChannel';
import { losNativeInterAd } from '../UI/losNativeInterAd';
import loskc from '../SDK/loskc';

export class losTools {
    public static showBox() {
        assetManager.loadBundle('boxReward', (err, bundle) => {
            bundle.load('prefab/popup/rewardBoxFrame', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    if (node.parent.getChildByName('box9UI')) {
                        log('已存在box9UI，销毁')
                        node.destroy()
                        return;
                    }
                    node.name = 'box9UI'
                    node.setSiblingIndex(99999)
                }
            })
        })
    }
    public static showLoginUI() {
        assetManager.loadBundle('StaminaSystem', (err, bundle) => {
            console.log(bundle)
            bundle.load('ui/Login', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    node.setSiblingIndex(99999)
                }
            })
        })
    }
    public static showLoginReward() {
        assetManager.loadBundle('StaminaSystem', (err, bundle) => {
            console.log(bundle)
            bundle.load('ui/LoginReward', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    node.setSiblingIndex(99999)
                }
            })
        })
    }
    public static showBuyStaminaUI() {
        assetManager.loadBundle('StaminaSystem', (err, bundle) => {
            console.log(bundle)
            bundle.load('ui/PowerShortage', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    node.setSiblingIndex(99999)
                }
            })
        })
    }
    public static showInfiniteStaminaUI() {
        assetManager.loadBundle('StaminaSystem', (err, bundle) => {
            console.log(bundle)
            bundle.load('ui/InfinitePower', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    node.setSiblingIndex(99999)
                }
            })
        })
    }
    public static showKuangDian(isOnCloseShowInter: boolean = false, siblingIndex: number = -1) {
        assetManager.loadBundle('kuangDian', (err, bundle) => {
            console.log(bundle)
            bundle.load('kuangDian', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    let s = node.getComponent("LosKuangDian") as losKuangDian
                    console.log(s)
                    if (s) {
                        s.setShowInter(isOnCloseShowInter);
                    }
                    node.parent = find("UICanvas")
                    const stageUp = find("UICanvas/stageUp")
                    node.setSiblingIndex(stageUp?.getSiblingIndex() ?? 99999);
                    if (siblingIndex != -1) {
                        node.setSiblingIndex(siblingIndex);
                    }
                }
            })
        })
    }
    public static showRewardBoxNew() {
        assetManager.loadBundle('boxRewardNew', (err, bundle) => {
            console.log(bundle)
            bundle.load('ui/newRewardBoxFrame', Prefab, (err, p) => {
                if (p != null) {
                    let node = instantiate(p)
                    node.parent = find("UICanvas")
                    if (node.parent.getChildByName('box9UInew')) {
                        log('已存在box9UInew，销毁')
                        node.destroy()
                        return;
                    }
                    node.name = 'box9UInew'
                    const stageUp = find("UICanvas/stageUp")
                    node.setSiblingIndex(stageUp.getSiblingIndex())
                }
            })
        })
    }

    public static showHwNativeAD(adid: string, picUri: string, title: string, source: string, clickBtnTxt: string) {
        resources.load('/NativeInterAd', Prefab, (err, data) => {
            if (err) {
                log(err)
                return;
            }
            loskc.log('实例化华为原生广告成功');
            let node = instantiate(data)
            let script = node.getComponent(losNativeInterAd);
            if (script) {
                script.init(adid, picUri, title, source, clickBtnTxt);
                loskc.log('初始化华为原生广告组件数据 成功');

                node.parent = find("UICanvas");
                node.setSiblingIndex(99999);
            }

        });
    }
    /**
     * 将秒数转换为 00:00 或 00:00:00 格式的时间字符串
     * @param seconds 剩余的总秒数
     * @param showHours 是否显示小时数，默认为 true
     * @returns 格式化后的时间字符串
     */
    public static formatSeconds(seconds: number, showHours: boolean = true): string {
        const hours = Math.floor(seconds / 3600);
        const minutes = Math.floor((seconds % 3600) / 60);
        const remainingSeconds = seconds % 60;

        if (showHours) {
            // 格式为 HH:MM:SS
            return `${this.formatTime(hours)}:${this.formatTime(minutes)}:${this.formatTime(remainingSeconds)}`;
        } else {
            // 格式为 MM:SS
            return `${this.formatTime(minutes)}:${this.formatTime(remainingSeconds)}`;
        }
    }
    public static isNewDay(timeA: number): boolean {
        if (timeA <= 0) {
            return true;
        }
        const d1 = new Date(timeA);
        const d2 = new Date();
        if (d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate()) {
            return false;
        }
        return true;
    }

    /**
     * 格式化数字为两位数
     * @param num 需要格式化的数字
     * @returns 格式化后的字符串
     */
    private static formatTime(num: number): string {
        return num < 10 ? `0${num}` : `${num}`;
    }
    /*生成min到max中间的随机数，包含min和max
    * @param min 
    * @param max 
    * @returns 
    */
    public static getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
}